#!/bin/bash

set -ux

# 安装containerd
curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo &&
    dnf makecache &&
    dnf install -y containerd.io

# 生成默认配置
mkdir -p /etc/containerd &&
    containerd config default >/etc/containerd/config.toml

# 配置 systemd cgroup 驱动程序
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml &&
    cat /etc/containerd/config.toml | grep 'SystemdCgroup'

# 修改pause镜像仓库
sed -i 's,registry.k8s.io\/pause:3.6,registry.aliyuncs.com\/google_containers\/pause:3.7,' /etc/containerd/config.toml &&
    cat /etc/containerd/config.toml | grep 'pause:'

# 添加国内仓库
sed -i \
    -e '/registry.mirrors]$/a \        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]' \
    -e '/registry.mirrors]$/a \          endpoint = [ "https://3otatncp.mirror.aliyuncs.com", "https://registry.cn-hangzhou.aliyuncs.com" ]' \
    -e '/registry.mirrors]$/a \        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"]' \
    -e '/registry.mirrors]$/a \          endpoint = [ "https://3otatncp.mirror.aliyuncs.com", "https://registry.cn-hangzhou.aliyuncs.com" ]' \
    -e '/registry.mirrors]$/a \        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]' \
    -e '/registry.mirrors]$/a \          endpoint = [ "https://3otatncp.mirror.aliyuncs.com", "https://registry.cn-hangzhou.aliyuncs.com" ]' \
    -e '/registry.mirrors]$/a \        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."quay.io"]' \
    -e '/registry.mirrors]$/a \          endpoint = [ "https://3otatncp.mirror.aliyuncs.com", "https://registry.cn-hangzhou.aliyuncs.com" ]' \
    /etc/containerd/config.toml &&
    cat /etc/containerd/config.toml | grep -A 10 'registry.mirrors'

# 设置开机自启
systemctl daemon-reload &&
    systemctl enable containerd &&
    systemctl start containerd

# crictl config --set runtime-endpoint="unix:///run/containerd/containerd.sock" &&
#     crictl config --set image-endpoint="unix:///run/containerd/containerd.sock" &&
#     cat /etc/crictl.yaml

# 清楚缓存
dnf clean all
